﻿<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%--<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>--%>
<%--<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>--%>
<%--动态包含无法使用，页面报错，newsService无法使用 <jsp:include page="../common/common.jsp" /> --%>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>无标题文档</title>
    <style type="text/css">
        table {
            margin: 20px;
        }

        .color {
            background: grey;
        }
    </style>
</head>
<body>
<!--主体-->
<div class="main-content-right">
    <!--即时新闻-->
    <div class="main-text-box">
        <div class="main-text-box-tbg">
            <div class="main-text-box-bbg">
                <form name="searchForm" id="searchForm" action="" method="">
                    <div>
                        新闻分类：
                        <select name="categoryId" id="categoryId">
                            <option value="-1">全部</option>

                            <option value='1'>国内</option>

                            <option value='2'>国际</option>

                            <option value='3'>娱乐</option>

                            <option value='4'>军事</option>

                            <option value='5'>财经</option>

                            <option value='6'>天气</option>

                        </select>
                        新闻标题<input type="text" name="title" id="title" value=''/>
                        <button type="button" class="page-btn" onclick="searchNews(1)">GO</button>
                        <button type="button" class="page-btn" onclick="addnews()">增加</button>
                        <input type="hidden" name="currentPageNo" value="1"/>
                        <input type="hidden" name="pageSize" value="10"/>
                        <input type="hidden" name="totalPageCount" value="2"/>

                    </div>
                </form>
                <table id="newsList" border="1" cellpadding="0" cellspacing="0" class="admin-list" style="width: 500px">
                    <thead>
                    <tr class="admin-list-head">
                        <th>新闻标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%--<c:forEach var="news" items="${list}" varStatus="status">--%>
                    <%--<tr <c:if test="${status.count%2==0}">class="color" </c:if>>--%>
                    <%--<td><a href="/newsServlet?action=getDetailById&id=${news.id}">${news.title}</a></td>--%>
                    <%--<td><c:out value="${news.author}" escapeXml="true"/></td>--%>
                    <%--<td><fmt:formatDate value="${news.createDate}" pattern="yyyy-MM-dd"/></td>--%>
                    <%--<td>--%>
                    <%--<a href="/newsServlet?action=getNewsById&id=${news.id}">编辑</a>&nbsp;&nbsp;&nbsp;--%>
                    <%--<a href="/newsServlet?action=delete&id=${news.id}">删除</a>--%>
                    <%--</td>--%>
                    <%--</tr>--%>
                    <%--</c:forEach>--%>
                    </tbody>
                </table>
                <div class="page-bar">
				<span>
					共<span id="total"></span>条记录&nbsp;&nbsp; <span id="pageNo"></span>/<span id="totalPages"></span>页&nbsp;&nbsp;&nbsp;&nbsp;
					<a href="javascript:void(0)" id="begin">首页</a>&nbsp;&nbsp;&nbsp;&nbsp;
					<a href="javascript:void(0)" id="prev">上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;
					<a href="javascript:void(0)" id="next">下一页</a>&nbsp;&nbsp;&nbsp;&nbsp;
					<a href="javascript:void(0)" id="end">最后一页</a>
				</span>
                    <span class="page-go-form"><label>跳转至</label>
	     <input type="text" name="inputPage" id="inputPage"/>页
	     <button type="button" id="page-btn" class="page-btn" >GO</button>
		</span>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
<script src="/js/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
    //初始化数据
    var currentPage=1;  //当前页码
    var pageSize=3; //每页多少条
    //过滤查询
function searchNews(currentPage) {
    $("#newsList tbody").html("");  //清空数据
    var categoryId = $("#categoryId").val();
    var title = $("#title").val();
    $.ajax({
        "url": "/newsServlet",
        "type": "post",
        "data": {
            "action": "select3",
            "currentPage": currentPage,
            "pageSize": pageSize,
            "categoryId": categoryId,
            "title": title
        },
        "dataType": "json",
        "success": callback,
        "error": function () {
            alert("请求异常，请与管理员联系")
        }
    })

    function callback(data) {
        // $(data).each(function () {
        //     $("#newsList").append("<tr><td>"+this.title+"</td><td>"+this.author+"</td><td>"+this.createDate+"</td><td>"+"<a href=\"newsDetailUpdateSimple.jsp?id="+this.id+"\">编辑</a>" +
        //         "<input type='button' value='删除'  onclick='delNewsById("+this.id+")'/>"+"</td></tr>")
        // })
        for (var i = 0; i < data.list.length; i++) {
            $("#newsList tbody").append("<tr>" +
                "<td>" + data.list[i].title + "</td>" +
                "<td>" + data.list[i].author + "</td>" +
                "<td>" + data.list[i].createDate + "</td>" +
                "<td>" +
                "<a href='newsDetailUpdateSimple.jsp?id=" + data.list[i].id + "'>编辑</a>" +
                "<input type='button' onclick='deleteNews(" + data.list[i].id + ",this)' value='删除'/>" +
                "</td>" +
                "</tr>")
        }
        // $("#newsList").append("<tr></tr>");
        $("tr:even").addClass("color");
        $("#total").html(data.totalCount);  //数据
        $("#totalPages").html(data.totalPages); //总页码
        $("#pageNo").html(currentPage);    //给当前页码赋值
    }
}
$(function () {
    //初始
    searchNews(currentPage);
    //首页
    $("#begin").click(function () {
        if (currentPage!=1){    //判断页码如果不等于1执行操作 如果等于1提示当前页已为首页
        $("#newsList tbody").html("");
        currentPage=1; //当前页码1
        searchNews(currentPage);
        $("#pageNo").html(currentPage);//给当前页码赋值
        }else {
            alert("当前页已为首页!")
            return;
        }
    })
    //上一页
    $("#prev").click(function () {
        if (currentPage>1){      //判断页码如果大于于1执行操作 如果等于1提示当前页已为首页
        $("#newsList tbody").html("");
        currentPage=currentPage-1;  //点击上一页 当前页码减一
        searchNews(currentPage);
        $("#pageNo").html(currentPage); //给当前页码赋值
        }else {
            alert("当前页面为首页!")
            return;
        }
    })
    //下一页
    $("#next").click(function () {
        if (currentPage<$("#totalPages").html()){
        $("#newsList tbody").html("");  //清空数据
        currentPage=currentPage+1;      //点击下一页 当前页码加一
        searchNews(currentPage);
        $("#pageNo").html(currentPage); //给当前页码赋值
        }else {
            alert("已经是最后一页!")
            return;
        }
    })
    //最后一页
    $("#end").click(function () {
        if (currentPage!=$("#totalPages").html()) { //判断如果是最后一页提示当前页已为最后一页
            $("#newsList tbody").html("");
            currentPage = $("#totalPages").html();    //当前页码等于总页数
            searchNews(currentPage);
            $("#pageNo").html(currentPage);//给当前页码赋值
        }else {
            alert("当前页已为最后一页!")
            return;
        }
    })
    //到第几页
    $("#page-btn").click(function () {
        currentPage=$("#inputPage").val();  //将输入的值赋给当前页面的值
        if (currentPage>$("#totalPages").html()||currentPage<1) {  //判读如果当前页面的值大于总页面的值 或者 当前页面值小于1 提示用户填写错误
            alert("填写有误!")
            // $("#inputPage").val("");
            return;
        }else {
            $("#newsList tbody").html("");
            searchNews(currentPage);
            $("#pageNo").html(currentPage);//给当前页码赋值
        }
    })
})
// $(function () {
//     $.post("/newsServlet","action=select2",callback,"html");
//     function callback(data) {
//         $("#newList").append(data);
//     }
//     // $("#newsList").load("/newsServlet","action=select1");
// });
function deleteNews(id) {
    $.ajax({
        "url":"/newsServlet",
        "type":"post",
        "data":"action=delete&id="+id,
        "dataType":"json",
        "success":delNewscallBack,
        "error":function () {
            alert("请求异常，请与管理员联系")
        }
    });
    function delNewscallBack(data) {
        if (data.flag=="true") {
            location.reload();
            // $(btn).parent().parent().remove();
            alert("操作成功")
        }else {
           alert("操作失败")
        }
    }
}
 function addnews() {
     location.href="newsDetailCreateSimple.jsp"
 }
</script>
</html>